package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.system.domain.Car;
import com.ruoyi.system.service.IWorkerService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Worker;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 【请填写功能名称】Controller
 *
 * @author ruoyi
 * @date 2024-04-07
 */
@RestController
@RequestMapping("/system/worker")
public class WorkerController extends BaseController
{
    @Autowired
    private IWorkerService workerService;

    /**
     * 查询【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:worker:list')")
    @GetMapping("/list")
    public TableDataInfo list(Worker worker)
    {
        startPage();
        List<Worker> list = workerService.selectWorkerList(worker);
        return getDataTable(list);
    }
    @Anonymous
    @GetMapping("/all")
    public AjaxResult getAll(Worker worker)
    {
        List<Worker> list = workerService.selectWorkerList(worker);
        return AjaxResult.success(list);
    }


    /**
     * 导出列表
     */
    @PreAuthorize("@ss.hasPermi('system:worker:export')")
    @Log(title = "员工数据导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Worker worker)
    {
        List<Worker> list = workerService.selectWorkerList(worker);
        ExcelUtil<Worker> util = new ExcelUtil<Worker>(Worker.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    @Log(title = "人员管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:worker:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<Worker> util = new ExcelUtil<>(Worker.class);
        List<Worker> workerList = util.importExcel(file.getInputStream());
        String message = workerService.importWorkers(workerList, updateSupport);
        return AjaxResult.success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<Worker> util = new ExcelUtil<>(Worker.class);
        util.importTemplateExcel(response, "员工数据");
    }

    /**
     * 获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:worker:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(workerService.selectWorkerById(id));
    }

    /**
     * 新增
     */
    @PreAuthorize("@ss.hasPermi('system:worker:add')")
    @Log(title = "新增员工信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Worker worker)
    {
        return toAjax(workerService.insertWorker(worker));
    }

    /**
     * 修改【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:worker:edit')")
    @Log(title = "修改员工信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Worker worker)
    {
        return toAjax(workerService.updateWorker(worker));
    }

    /**
     * 删除【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:worker:remove')")
    @Log(title = "删除员工信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(workerService.deleteWorkerByIds(ids));
    }
}
